require "wx"
require("stab.scripts.init.wxUtil")
local wxGUI = require("stab.scripts.init.wxGUI")
local wxFrame = require("stab.scripts.init.wxFrame")
local wxTree = require("stab.scripts.init.wxTree")
local wxMenu = require("stab.scripts.init.wxMenu")
local wxNodePanel = require("stab.scripts.init.wxNodePanel")

local DEFAULT_WIDTH = 550
local DEFAULT_HEIGHT = 950
local sceneTree = class("sceneTree")
function sceneTree:ctor()
    if sceneTree.instance ~= nil then
        return
    end
    self:initializeFrame()
end

function sceneTree:initializeFrame()
    if (type(require("wx")) == "boolean") or not wx then
        print("重启客户端生效!")
        return
    end
    self.infoData = {
        interval = 1 / 20,
        chooseNode = nil
    }
    self.cameras = {}
    self.pause = false
    local this = self
    self.wxFrame =
        wxFrame.create(
        {
            title = "wxFrame",
            width = DEFAULT_WIDTH,
            height = DEFAULT_HEIGHT,
            callback = function()
                this:Close()
            end
        }
    )
    self.frameLayout = wxGUI:createGridLayout(2, 1, 0, 0)
    self.wxFrame:getFrame():SetSizer(self.frameLayout)
    self.wxTreeFrame = self.wxFrame
    self.wxNodeFrame = self.wxFrame
    self:createSceneTree()
    self:createNodePanle()
    self:createMenuBar()
    self:createTimer(
        1 / 20,
        function()
            self:Tick()
        end
    )
    self.frameLayout:AddGrowableRow(0, 0)
    self.frameLayout:AddGrowableRow(1, 0)
    self.frameLayout:AddGrowableCol(0, 0)
end

function sceneTree:createMenuBar()
    self.wxMenuBar =
        wxMenu.create(
        {
            frame = self.wxFrame:getFrame()
        }
    )
end

function sceneTree:createTimer(interval, callback)
    self.infoData.interval = interval
    self.timer = wx.wxTimer(self.wxFrame:getFrame())
    local lastTime = os.clock()
    function onTimer(event)
        if (os.clock() - lastTime) >= self.infoData.interval then
            lastTime = os.clock()
            if callback then
                callback()
            end
        end
    end
    self.wxFrame:getFrame():Connect(
        wx.wxEVT_TIMER,
        function(event)
            onTimer(event)
        end
    )
    self.timer:Start()
end

function sceneTree:createSceneTree()
    self.wxTree =
        wxTree.create(
        {
            frame = self.wxTreeFrame:getFrame(),
            width = 0,
            height = 0,
            cameras = self.cameras,
            infoData = self.infoData,
            callBack = function(data)
                if data then
                    self.infoData.chooseNode = data.itemData.node
                else
                    self.infoData.chooseNode = nil
                end
            end
        }
    )

    self.frameLayout:Add(self.wxTree.tree, 0, wxOr(wx.wxEXPAND, wx.wxALL))
    self.input = ""
end

function sceneTree:createNodePanle()
    self.wxNodePanel =
        wxNodePanel.create(
        {
            frame = self.wxNodeFrame:getFrame(),
            width = 0,
            height = 0,
            cameras = self.cameras,
            infoData = self.infoData,
            callBack = function(node)
                if self.infoData.chooseNode ~= node then
                    self.infoData.chooseNode = node
                    self.wxTree:wxCalculateOpenState()
                end
            end
        }
    )
    self.frameLayout:Add(self.wxNodePanel.nodePanel, 0, wxOr(wx.wxEXPAND, wx.wxALL))
end

function sceneTree:getInstance()
    if sceneTree.instance == nil then
        sceneTree.instance = sceneTree.new()
    end
    return sceneTree.instance
end

function sceneTree:destroy()
    self.frameLayout = nil
    self.wxNodePanel = nil
    self.wxMenuBar = nil
    self.wxNodeFrame = nil
    self.wxFrame = nil
    self.timer = nil
    sceneTree.instance = nil
end

function sceneTree:Tick()
    if not self.pause then
        if self.wxTreeFrame:getState() and self.wxTree then
            local scenes = self:calculateTreeInfo()
            self.wxTree:wxRefreshTree(scenes)
        end
        if self.wxNodeFrame:getState() then
            self.wxNodePanel:refreshNode()
        end
    end
end

function sceneTree:calculateTreeInfo()
    local scene = cc.Director:getInstance():getRunningScene()
    if not self.input or self.input == "" then
        return {scene}
    end
    --计算同名child
    local nodes = {}
    local deepSearch
    deepSearch = function(children, name)
        if children and next(children) then
            for i, node in ipairs(children) do
                if node.name == name then
                    tbale.table.insert(nodes, node)
                end
                deepSearch(node:getchildren(), name)
            end
        end
    end
    deepSearch({scene}, self.input)
    return nodes
end

function sceneTree:Open()
    self.wxFrame:Open()
end

function sceneTree:Close()
    self:destroy()
    os.exit()
end

sceneTree.instance = nil

return sceneTree
